package com.yang.mall.service.impl;

import com.yang.mall.dao.CategoryMapper;
import com.yang.mall.pojo.Category;
import com.yang.mall.service.ICategoryService;
import com.yang.mall.vo.CategoryVo;
import com.yang.mall.vo.ResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.yang.mall.consts.MallConst.ROOT_PARENT_ID;

@Service
public class CategoryServiceImpl implements ICategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ResponseVo<List<CategoryVo>> selectAll() {
        List<Category> categories = categoryMapper.selectAll();
        // 查出parentId=0的数据作为一级父目录
        // TODO: 2022/7/18 了解学习 lambda+stream 输出
        List<CategoryVo> categoryVoList = categories.stream()
                .filter(e -> e.getParentId().equals(ROOT_PARENT_ID))
                .map(this::category2CategoryVo)
                .sorted(Comparator.comparing(CategoryVo::getSortOrder).reversed())
                .collect(Collectors.toList());
        // 查子目录
        findSubCategory(categoryVoList, categories);
        return ResponseVo.success(categoryVoList);
    }

    @Override
    public void findSubCategoryId(Integer id, Set<Integer> result) {
        List<Category> categories = categoryMapper.selectAll();
        findSubCategoryId( id, result, categories);
    }

    private void findSubCategoryId(Integer id, Set<Integer> result, List<Category> categories) {
        for (Category category : categories) {
            if (category.getParentId().equals(id)){
                result.add(category.getId());
                findSubCategoryId(category.getId(), result, categories);
            }
        }
    }


    // 取子目录内信息可以反复迭代直到没有子目录
    private void findSubCategory(List<CategoryVo> categoryVoList, List<Category> categories){
        // 取出父目录
        for (CategoryVo categoryVo : categoryVoList) {
            List<CategoryVo> subCategoryVoList = new ArrayList<>();
            // 取出子目录
            for (Category category : categories) {
                if(categoryVo.getId().equals(category.getParentId())){
                    CategoryVo subCategoryVo = category2CategoryVo(category);
                    subCategoryVoList.add(subCategoryVo);
                }
                // 按照从小到大排序
                subCategoryVoList.sort(Comparator.comparing(CategoryVo::getSortOrder).reversed());
                categoryVo.setCategoryVo(subCategoryVoList);
                // 如果还有子目录那就继续往下查
                findSubCategory(subCategoryVoList, categories);
            }
        }
    }

    // 转换容器格式
    private CategoryVo category2CategoryVo(Category category) {
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category, categoryVo);

        return categoryVo;
    }
}
